﻿namespace MicroCloud.Utils
{
    /// <summary>
    /// 树形算法帮助类
    /// </summary>
    public class TreeHelper
    {
        /// <summary>
        /// 树形路径字符串格式
        /// </summary>
        private const string _treePathStringFormat = "${0}$";

        /// <summary>
        /// 格式化树形路径字符串
        /// </summary>
        /// <typeparam name="T">值类型</typeparam>
        /// <param name="value">要格式化的值</param>
        /// <returns>树形路径字符串</returns>
        public static string FormatTreePathString<T>(T value)
            where T : IEquatable<T>
        {
            var treePathString = FormatTreePathString(value, _treePathStringFormat);
            return treePathString;
        }
     
        /// <summary>
        /// 格式化树形路径字符串
        /// </summary>
        /// <typeparam name="T">值类型</typeparam>
        /// <param name="value">要格式化的值</param>
        /// <param name="format">树形路径字符串格式，形如 "${0}$"。</param>
        /// <returns>树形路径字符串</returns>
        public static string FormatTreePathString<T>(T value, string format)
            where T : IEquatable<T>
        {
            if (format.IsMissing())
            {
                format = _treePathStringFormat;
            }
            var treePathString = format.FormatWith(value);
            return treePathString;
        }

        /// <summary>
        /// 获取树形路径字符串
        /// </summary>
        /// <typeparam name="T">值类型</typeparam>
        /// <param name="value">要格式化的值</param>
        /// <param name="parentTreePathString">父级树形路径字符串</param>
        /// <returns>树形路径字符串</returns>
        public static string GetTreePathString<T>(T value, string parentTreePathString)
            where T : IEquatable<T>
        {
            var treePathString = GetTreePathString(value, parentTreePathString, _treePathStringFormat);
            return treePathString;
        }

        /// <summary>
        /// 获取树形路径字符串
        /// </summary>
        /// <typeparam name="T">值类型</typeparam>
        /// <param name="value">要格式化的值</param>
        /// <param name="parentTreePathString">父级树形路径字符串</param>
        /// <param name="format">树形路径字符串格式，形如 "${0}$"。</param>
        /// <returns>树形路径字符串</returns>
        public static string GetTreePathString<T>(T value, string parentTreePathString, string format)
            where T : IEquatable<T>
        {
            Check.NotNullOrEmpty(parentTreePathString, nameof(parentTreePathString));

            var treePathString = $"{parentTreePathString},{FormatTreePathString(value, format)}";
            return treePathString;
        }

    }
}
